# encoding=utf-8
import re

'''
重新整理文本 
'''

# bibile 属性

part = ''	#新旧约（新约/旧约）
book = ''	#圣经卷
chapter = ''	#章
section = ''	#节
message = ''	#经文内容
post_date = '2009-11-15T14:12:12'	#索引创建时间
author = ''	#作者
timeline = ''	#事件发生时间
book_type = ''	#书卷类型（摩西五经，先知书，智慧书……）

source_file = open("/Users/luke/oschina/jieba-demo/txt/bible-doc") 
target_file = open('/Users/luke/oschina/jieba-demo/txt/bible-doc-message.json','w')
while 1:
  lines = source_file.readlines(10000)
  if not lines:
    break
  for line in lines:
  	# 按行对原文进行处理
  	if re.match('^新约.*$',line) is not None or re.match('^旧约.*$',line) is not None:
  		part = line.replace("\n","").replace("\r","")
  	elif re.match('^(?!\d).*$',line) is not None:
		book = line.replace("\n","").replace("\r","")
	else:
		#对圣经章节及内容进行折分
		chapter = line.split("  ")[0].split(":")[0]
		section = line.split("  ")[0].split(":")[1]
		message = line.split("  ")[1]
		#将整理后的文本写到文件

		v_part = "\"part\":\"" + part + "\""
		v_book = "\"book\":\"" + book + "\""
		v_chapter = "\"chapter\":\"" + chapter + "\""
		v_section = "\"section\":\"" + section + "\""
		v_message = "\"message\":\"" + message.replace("\n","").replace("\r","") + "\""
		v_post_date = "\"post_date\":\"" + post_date + "\""

		line = "{" + " , ".join([v_part,v_book,v_chapter,v_section,v_message,v_post_date]) + "}\n"

		#target_file.writelines('''{ "index" : { "_index" : "twitter", "_type" : "_doc"} }\n''')
		target_file.writelines(line)

		'''
		# 验证文本格式
		if len(line.split("  ")) == 2:
			target_file.writelines(line);
		'''
#在组装向es里导入数据的json文件时必须最后要有一行空行
#target_file.writelines("\n")

source_file.close()
target_file.close()

'''
将整理好的文本导入es中 
'''